﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mobile.Models;
using Mobile.MainService;

namespace Mobile.Controllers
{
    public class AdminUIController : Controller
    {
        Service sv = new Service();
        Party pt = new Party();



        // POST: /GuestUI
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            GuestUIController x = new GuestUIController();
            string accessCode = collection.Get("partyCode");
            string adminID = collection.Get("adminID");
            string partyID = null;
            if (accessCode != null)
            {
                partyID = x.GetPartyID(accessCode);
            }
            if (String.IsNullOrEmpty(partyID))
            {
                partyID = "55f55676-a894-4b65-8060-5c93c649efa2";
            }
            Session["PartyID"] = partyID;

            return RedirectToAction("Playlist","AdminUI", new{ id=partyID });
        }

        public PartialViewResult GetNowPlaying()
        {
            string partyID = getSessionPartyID();

            if (String.IsNullOrEmpty(partyID))
                throw new Exception("No PartyID in GetNowPlaying");
            return PartialView("_GetNowPlaying", new Queue { partyID = partyID });
        }

        public PartialViewResult GetQueue()
        {
            string partyID = getSessionPartyID();

            if (String.IsNullOrEmpty(partyID))
                throw new Exception("No PartyID in GetQueue");

            Response.AddHeader("Refresh", "10");
            return PartialView("_Playlist", new Queue { partyID = partyID });
        }
        /// <summary>
        /// Admin is given ability to also add a song from database
        /// </summary>
        /// <param name="partyID"></param>
        /// <returns></returns>
        public ActionResult AddSong()
        {
            string partyID = getSessionPartyID();
            if (String.IsNullOrEmpty(partyID))
                throw new Exception("No PartyID in AddSong");

            return View(new Party { PartyID = partyID });
        }

        /// <summary>
        /// Admin has ability to skip a song
        /// </summary>
        /// <param name="partyID"></param>
        /// <returns></returns>
        public ActionResult SkipSong()
        {
            string partyID = getSessionPartyID();

            if (String.IsNullOrEmpty(partyID))
                throw new Exception("No PartyID in SkipSong");

            //pt = new Party { PartyID = partyID, Description = partyID, EndTime = new DateTime(), queue = new Queue { partyID = partyID } };
            //string newSong = pt.queue.SongList.ElementAt(0).ID;
            //sv.UpdateNowPlaying(partyID, newSong);
            
            return View("Playlist", new Queue { partyID = partyID });
        }

        /// <summary>
        /// This Method returns the party's playlist
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public ActionResult Playlist()
        {
            string partyID = getSessionPartyID();

            if (String.IsNullOrEmpty(partyID))
                throw new Exception("No PartyID in Playlist");

            return View(new Queue { partyID = partyID });
        }

        /* DownvoteButton Action Method
       * 
       * Desc: Takes Song ID from ActionLink call and tells service to downvote
       * 
       * @param string SongID: the songID that has been updated
       * @return: partial view Queue
       */
        public ActionResult DownvoteButton(string songID)
        {
            string partyID = getSessionPartyID();
            if (String.IsNullOrEmpty(partyID))
                throw new Exception("No PartyID in UpvoteButton");

            sv.DownvoteSong(partyID, songID);

            return View("Playlist", new Queue { partyID = partyID });
        }

        /// <summary>
        /// Admin will get the upvote option as well
        /// </summary>
        /// <param name="songID"></param>
        /// <param name="partyID"></param>
        /// <returns></returns>
        public ActionResult UpvoteButton(string songID)
        {
            string partyID = getSessionPartyID();

            if (String.IsNullOrEmpty(partyID))
                throw new Exception("No PartyID in UpvoteButton");

            sv.UpvoteSong(partyID, songID);

            return View("Playlist", new Queue { partyID = partyID });
        }

        public string getSessionPartyID()
        {
            string id = (string) Session["PartyID"];
            if (string.IsNullOrEmpty(id))
            {
               // RedirectToActionPermanent("Index", "Home", new { message = "Please provide a correct PartyID" });
            }

            return id;
        }

    }
}
